package enterprise

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetEnterpriseDetailByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetEnterpriseDetailByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetEnterpriseDetailByIdApiLogic {
	hostname, _ := os.Hostname()
	return &GetEnterpriseDetailByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetEnterpriseDetailByIdApiLogic) GetEnterpriseDetailByIdApi(req *types.IdReq) (resp *types.EnterpriseModel, err error) {
	// todo: add your logic here and delete this line
	enterpriseEntity, err := dao.EnterpriseEntity.WithContext(l.ctx).Where(dao.EnterpriseEntity.ID.Eq(req.Id)).First()
	if err != nil {
		return nil, err
	}
	return &types.EnterpriseModel{
		ID:           enterpriseEntity.ID,                                 // 主键id
		Name:         enterpriseEntity.Name,                               // 企业名称
		Person:       enterpriseEntity.Person,                             // 负责人
		Mobile:       enterpriseEntity.Mobile,                             // 联系电话
		Logo:         enterpriseEntity.Logo,                               // 企业logo
		Status:       enterpriseEntity.Status,                             // 状态0：营业  1：停业
		ProvinceCode: enterpriseEntity.ProvinceCode,                       // 省份id
		CityCode:     enterpriseEntity.CityCode,                           // 城市id
		CountyCode:   enterpriseEntity.CountyCode,                         // 县、区id
		TownCode:     enterpriseEntity.TownCode,                           // 乡镇code
		Address:      enterpriseEntity.Address,                            // 具体地址
		FullAddress:  enterpriseEntity.FullAddress,                        // 拼接省市县地址
		CreatedAt:    utils.TimestampToString(enterpriseEntity.CreatedAt), // 创建时间
		UpdatedAt:    utils.TimestampToString(enterpriseEntity.UpdatedAt), // 更新时间
	}, nil
}
